package com.lemner.ke.activity;

import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemner.ke.R;
import com.lemner.ke.base.BaseActivity;
import com.lemner.ke.base.BaseListener;
import com.lemner.ke.base.BaseObjectBean;
import com.lemner.ke.base.SuccessListener;
import com.lemner.ke.bean.SuccessBean;
import com.lemner.ke.model.fail.FailModel;
import com.lemner.ke.model.loginreg.GetIdentifyingCodeModel;
import com.lemner.ke.model.loginreg.RegisterModel;
import com.lemner.ke.model.loginreg.VerifyPhoneModel;
import com.lemner.ke.util.Phone;

import java.util.Timer;
import java.util.TimerTask;

/**

* 作者：张恺

* 时间：2018/9/25

* 类描述：注册页面

*/

public class RegisterActivity extends BaseActivity {
    ImageView iv_back, iv_clear;
    EditText et_phone, et_ver_code, et_name, et_pwd, et_pwd_again;
    TextView tv_ver_send;
    Button btn_reg;
    private int time;

    @Override
    public int getLayoutFile() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        iv_back = findViewById(R.id.iv_reg_back);
        et_phone = findViewById(R.id.et_reg_phone);
        et_ver_code = findViewById(R.id.et_verification_code);
        et_name = findViewById(R.id.et_reg_name);
        et_pwd = findViewById(R.id.et_password1);
        et_pwd_again = findViewById(R.id.et_password2);
        iv_clear = findViewById(R.id.iv_reg_clear);
        tv_ver_send = findViewById(R.id.tv_verification_send);
        btn_reg = findViewById(R.id.btn_reg);
    }

    @Override
    public void initEvent() {
        iv_back.setOnClickListener(this);
        tv_ver_send.setOnClickListener(this);
        iv_clear.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!et_phone.getText().toString().equals("")) {
                    btn_reg.setBackgroundResource(R.drawable.change_phone_button);
                    iv_clear.setVisibility(View.VISIBLE);
                } else {
                    btn_reg.setBackgroundResource(R.drawable.setting_button);
                    iv_clear.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initSetting() {
        setTopBarEnable(false);
    }

    @Override
    public void onClick(View v) {
        final String mobile = et_phone.getText().toString();
        final String code = et_ver_code.getText().toString();
        final String name = et_name.getText().toString();
        final String pwd1 = et_pwd.getText().toString();
        final String pwd2 = et_pwd_again.toString();
        switch (v.getId()) {
            case R.id.iv_reg_back:
                finish();
                break;
            case R.id.iv_reg_clear:
                et_phone.setText("");
                break;
            case R.id.tv_verification_send:
                if (mobile.isEmpty()) {
                    showToast("请输入手机号！");
                } else if (!Phone.isMobile(mobile)) {
                    showToast("非法手机号，请重新输入！");
                } else {
                    new GetIdentifyingCodeModel().getIdentifyingCode(mobile, "identity", new SuccessListener());
                    time = 60;
                    final Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (time <= 0) {
                                        tv_ver_send.setText("发送验证码");
                                        tv_ver_send.setClickable(true);
                                        tv_ver_send.setTextColor(0xff000000);
                                        timer.cancel();
                                    } else {
                                        tv_ver_send.setText("重新发送(" + time-- + "s)");
                                        tv_ver_send.setClickable(false);
                                        tv_ver_send.setTextColor(0xff818181);
                                    }
                                }
                            });
                        }
                    }, 0, 1000);
                }
                break;
            case R.id.btn_reg:
                if (mobile.isEmpty()) {
                    showToast("请输入手机号！");
                } else if (name.isEmpty()) {
                    showToast("请输入昵称！");
                } else if (pwd1.isEmpty()) {
                    showToast("请输入密码！");
                } else if (pwd2.isEmpty()) {
                    showToast("请输入确认密码！");
                } else if (code.isEmpty()) {
                    showToast("请输入验证码！");
                } else {
                    if (pwd1.length() < 6 && pwd2.length() < 6) {
                        showToast("密码长度不能少于6位！");
                    } else if (code.length() < 6) {
                        showToast("验证码错误！");
                    } else if (!pwd1.equals(pwd2)) {
                        showToast("两次密码输入不一致！");
                    } else {
                        new VerifyPhoneModel().verifyPhone(mobile,code, new BaseListener<SuccessBean>() {
                            @Override
                            public void onResponse(SuccessBean successBean) {
                                new RegisterModel().register(mobile, pwd1, name, new BaseListener<SuccessBean>() {
                                    @Override
                                    public void onResponse(SuccessBean successBean) {
                                        showToast(successBean.getMsg());
                                        startActivity(LoginActivity.class);
                                        finish();
                                    }

                                    @Override
                                    public void onFail(String msg) {
                                        new FailModel().register(mobile, pwd1, name, new BaseListener<BaseObjectBean<String>>() {
                                            @Override
                                            public void onResponse(BaseObjectBean<String> stringBaseObjectBean) {
                                                showToast(stringBaseObjectBean.getMsg());
                                            }

                                            @Override
                                            public void onFail(String msg) {
                                                showToast(msg);
                                            }
                                        });
                                    }
                                });
                            }

                            @Override
                            public void onFail(String msg) {
                                showToast(msg);
                            }
                        });
                    }
                    break;

                }
        }
    }
}